package com.hiof.overlays;

import java.util.ArrayList;

//import no.hiof.edu.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;

import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.hiof.R;
import com.hiof.servercommunication.receive.beans.GeoPoint;
import com.hiof.utility.Utility;

public class TripRouteOverlay extends Overlay {
    Context context;

    public TripRouteOverlay(Context context) {
        this.context = context;
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean b) {
        super.draw(canvas, mapView, false);
        drawPath(canvas, mapView);
    }

    /*
      * Draw the route of the Trip
      */
    public void drawPath(Canvas canvas, MapView v) {
        int zoomLevel = ((MapView) ((Activity) context).findViewById(R.id.mapview)).getZoomLevel();
        MapPointsManager mapPointsManager = MapPointsManager.getInstane();
        int mapPointSize = mapPointsManager.getRouteMapSize();
        if (mapPointSize > 1) {
            Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
            p.setStrokeWidth(5);
            p.setColor(Color.BLUE);
            p.setStrokeMiter(100);
            p.setStyle(Style.STROKE);
            p.setAntiAlias(true);
            Point p1 = new Point();
            Point p2 = new Point();
            int increment = getLODIncrement(zoomLevel, mapPointSize);
            for (int i = 0; i < mapPointsManager.getRouteMapSize() - increment; i = i + increment) {
                v.getProjection().toPixels(mapPointsManager.getGoogleGeoPoint(i), p1);
                v.getProjection().toPixels(mapPointsManager.getGoogleGeoPoint(i + increment), p2);
                if (isInsideCanvas(canvas, p1.x, p1.y)
                        || isInsideCanvas(canvas, p2.x, p2.y)) {
                    canvas.drawLine(p1.x, p1.y, p2.x, p2.y, p);
                }
            }
        }
    }

    /*
      * Checks whether the given projected point lies inside canvas of screen or not.
      * It helps to control the plotting of route that lies outside the screen
      */
    public boolean isInsideCanvas(Canvas canvas, int x, int y) {
        return (x >= 0 && x <= canvas.getWidth() && y >= 0 && y <= canvas
                .getHeight());
    }

    /*
      *	LOD like implementation to reduce the number of points to
      *	be plotted when the zoom level of map is low
      *
      */
    private int getLODIncrement(int zoomLevel, int numPoints) {
//		do not change the values until necessary
        int returnValue = 2;
        if (zoomLevel < 8) {
            returnValue = numPoints;
        } else if (zoomLevel < 11) {
            returnValue = numPoints / 2;
        } else if (zoomLevel < 12) {
            returnValue = 3;
        }
        return returnValue - 1;
    }
}
